package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.GoodsConverter;
import com.cskaoyan.wordmemorize.dao.entity.GoodsDO;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dao.mapper.GoodsMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocCategoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.apache.ibatis.executor.BatchExecutorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 顾海龙
 * @version 1.0
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsDO> implements GoodsService {

    @Autowired
    GoodsConverter goodsConverter;

    @Autowired
    GoodsMapper goodsMapper;
    // 分页获取商品信息(后台商品管理)
    @Override
    public PageDTO<GoodsDTO> getGoodsPage(PageRequest request) throws BusinessException {
        PageDTO<GoodsDTO> pageDTO;
        QueryWrapper<GoodsDO> goodsDOQueryWrapper = new QueryWrapper<>();
        if (request.getKeyword() != null && !("".equals(request.getKeyword()))){
            goodsDOQueryWrapper.like("name",request.getKeyword());
        }
        if (request.getPageNum() == null || request.getPageNum() < 0){
            List<GoodsDO> list = this.list();
            pageDTO = goodsConverter.goodsPage2PageDTO(list,(long)list.size());
        }
        else {
            Page<GoodsDO> goodsDOPage = new Page<>(request.getPageNum(),request.getCount());
            Page<GoodsDO> goodsDOPageList = goodsMapper.selectPage(goodsDOPage, goodsDOQueryWrapper);
            pageDTO = goodsConverter.goodsPage2PageDTO(goodsDOPageList.getRecords(),goodsDOPageList.getTotal());
        }
        return pageDTO;
    }

    // 添加商品(后台商品管理)
    //type为空待处理
    @Override
    public void addGoods(GoodsCommand command) throws BusinessException {
        GoodsDO goodsDO = goodsConverter.command2GoodsDO(command);
        this.save(goodsDO);
    }

    // 更新商品(后台商品管理)
    @Override
    public void updateGoods(GoodsCommand command) throws BusinessException{
        GoodsDO goodsDO = goodsConverter.command2GoodsDO(command);
        QueryWrapper<GoodsDO> goodsDOQueryWrapper = new QueryWrapper<>();
        goodsDOQueryWrapper.eq("id",goodsDO.getId());
        this.update(goodsDO,goodsDOQueryWrapper);
    }

    // 删除商品(后台商品管理)
    @Override
    public DeleteGoodsOperateDTO deleteGoods(Long goodsId) throws BusinessException{
        GoodsDO byId = this.getById(goodsId);
        DeleteGoodsOperateDTO deleteGoodsOperateDTO = new DeleteGoodsOperateDTO();
        deleteGoodsOperateDTO.setId(byId.getId());
        deleteGoodsOperateDTO.setName(byId.getName());
        this.removeById(goodsId);
        return deleteGoodsOperateDTO;
    }

    // 批量删除指定商品(后台商品管理)
    @Override
    public List<DeleteGoodsOperateDTO> deleteGoodsSelected(BatchDeleteCommand command) throws BusinessException{
        List<GoodsDO> goodsDOS = goodsMapper.selectByIds(command.getIds());
        List<DeleteGoodsOperateDTO> deleteGoodsOperateDTOList = new ArrayList<>();
        for (GoodsDO goodsDO : goodsDOS) {
            DeleteGoodsOperateDTO deleteGoodsOperateDTO = new DeleteGoodsOperateDTO();
            deleteGoodsOperateDTO.setId(goodsDO.getId());
            deleteGoodsOperateDTO.setName(goodsDO.getName());
            deleteGoodsOperateDTOList.add(deleteGoodsOperateDTO);
        }
        this.removeByIds(command.getIds());
        return deleteGoodsOperateDTOList;
    }

    // 更新商品上下架状态(后台商品管理)
    @Override
    public void updateGoodsStatus(Long goodsId, Integer status) throws BusinessException{
        QueryWrapper<GoodsDO> goodsDOQueryWrapper = new QueryWrapper<>();
        goodsDOQueryWrapper.eq("id",goodsId);
        GoodsDO goodsDO = new GoodsDO();
        goodsDO.setStatus(status);
        this.update(goodsDO,goodsDOQueryWrapper);
    }

    // App端分页获取商品信息
    @Override
    public PageDTO<GoodsDTO> appGetGoodsPage(GoodsPageRequest goodsPageRequest) throws BusinessException {
        PageDTO<GoodsDTO> pageDTO;
        QueryWrapper<GoodsDO> goodsDOQueryWrapper = new QueryWrapper<>();
        if (goodsPageRequest.getKeyword() != null && !("".equals(goodsPageRequest.getKeyword()))){
            goodsDOQueryWrapper.like("name",goodsPageRequest.getKeyword());
        }
        if (goodsPageRequest.getTypes() != null && goodsPageRequest.getTypes().size() > 0){
            goodsDOQueryWrapper.in("type",goodsPageRequest.getTypes());
        }
        if (goodsPageRequest.getPageNum() == null || goodsPageRequest.getPageNum() <= 0){
            List<GoodsDO> list = this.list(goodsDOQueryWrapper);
            pageDTO = goodsConverter.goodsPage2PageDTO(list,(long)list.size());
        }
        else {
            Page<GoodsDO> goodsDOPage = new Page<>(goodsPageRequest.getPageNum(),goodsPageRequest.getCount());
            Page<GoodsDO> goodsDOPageList = goodsMapper.selectPage(goodsDOPage, goodsDOQueryWrapper);
            pageDTO = goodsConverter.goodsPage2PageDTO(goodsDOPageList.getRecords(),goodsDOPageList.getTotal());
        }
        return pageDTO;
    }
}
